﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WinService
{
    /// <summary>
    /// 简单的日志
    /// </summary>
    public class Logger : IDisposable
    {
        string _file;
        public Logger(string fileName)
        {
            var loc = typeof(Logger).Assembly.Location;
            var path = Path.GetDirectoryName(loc);
            _file = Path.Combine(path, fileName);
        }

        public void Dispose()
        {
            _writer.Flush();
            _writer.Close();
            _writer.Dispose();
        }

        StreamWriter _writer;

        /// <summary>
        /// 写入日志
        /// </summary>
        /// <param name="log"></param>
        public void Log(string log)
        {
            if (_writer == null)
            {
                _writer = new StreamWriter(_file);
            }

            _writer.WriteLine($"{DateTime.Now}:" + log);

            _writer.Flush();
        }
    }
}
